<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/4/21
 * Time: 19:58
 */

namespace app\admin\controller;


use app\common\model\SysUser;
use think\Exception;

class Manager extends Admin
{
    /**
     * 获取管理员列表
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function managerList()
    {
        $page = input('page',1);
        $limit = input('limit',20);
        $words = input('words','','trim');
        $where = [];
        if (!empty($words)){
            $where['username|true_name|phone'] = ['like', '%'.$words.'%'];
        }
        $model =new SysUser();
        $count =  $model->where($where)->count();
        if($page > ceil($count/$limit)){
            $page = ceil($count/$limit);
        }
        $lst = $model->with(['role'])
            ->field('user_id,username,phone,true_name,user_type,role_id,create_time')
            ->where($where)->page($page,$limit)->order('user_id desc,create_time desc')->select();
        return json_tab_msg($count,$page, $lst);
    }

    /**
     * 新增管理员
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function modifyUser()
    {
        $uid = input('user_id', 0);
        $data['username'] = input('username');
        $pwd= input('newPsw');
        $data['true_name'] = input('true_name');
        $data['phone'] = input('phone');
        if(!$uid){
            $data['password'] = md5($pwd);
        }
        $data['role_id'] = input('role_id',0);
        $daytime = date('Y-m-d H:i:s');
        $model =new SysUser();
        if($uid){ //修改用户信息
            $result = $this->validate($data,'Manager.update');
            if ($result !== true) json_return_code('',$result,201);
            $data['update_time'] = $daytime;
            $res = $model->save($data,array('user_id'=>$uid));
            $msg = '修改成功';
            addLog('修改用户信息');
        }else{ //新增用户信息
            $result = $this->validate($data,'Manager.add');
            if ($result !== true) json_return_code('',$result,201);
            $data['create_time'] = $daytime;
            $uid = $model->insertGetId($data);
            addLog('新增用户信息');
            $msg = '添加成功';
        }
        json_return_code($msg);
    }


    /**
     * 后台管理用户密码重置
     * @return \think\response\Json
     */
    public function resetPsw()
    {
        $id = input('id');
        if(!$id){
            json_return_code('','请选择用户!',201);
        }
        $model =new SysUser();
        $res = $model->save(array('password'=>md5('123456')), array('user_id'=>$id));
        if($res !== false){
            json_return_code('密码重置成功!');
        }else{
            json_return_code('','密码重置失败，请重试!',201);
        }
    }


    /***
     * 修改密码
     */
    public function updatePassword(){
        $org_pwd = input('oldPsw');
        $new_pwd = input('newPsw');
        $userid = input('user_id');
        if (empty($userid)){
            json_return_code('','用户id不能为空，请检查！',201);
        }
        if(empty($org_pwd)){
            json_return_code('','原密码不能为空，请检查！',201);
        }
        if(empty($new_pwd)){
            json_return_code('','新密码不能为空，请检查！',201);
        }

        $model = new SysUser();
        $user = $model->where(array('user_id'=>$userid))->find();
        if($user['password'] != md5($org_pwd) ){
            json_return_code('','原密码不正确，请检查！',201);
        }
        $res = $model->where(array('user_id'=>$userid))->update(array('password'=>md5($new_pwd)));
        json_return_code($res);
    }


    /**
     * 后台管理用户删除
     * @return \think\response\Json
     */
    public function delete()
    {
        $id = input('id');
        if(!$id){
            json_return_code('','用户id不能为空，请检查！',201);
        }
        $model =new SysUser();
        try {
            $model::destroy($id);
            json_return_code( '删除成功!');
        }catch (Exception $e){
            json_return_code('','删除失败，请重试!'.$e->getMessage(),201);
        }
    }
}